package com.shushan.util;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.huawei.android.hms.agent.HMSAgent;
import com.huawei.android.hms.agent.pay.PaySignUtil;
import com.huawei.android.hms.agent.pay.handler.PayHandler;
import com.huawei.hms.support.api.entity.pay.PayReq;
import com.huawei.hms.support.api.entity.pay.PayStatusCodes;
import com.huawei.hms.support.api.pay.PayResultInfo;
import com.nearme.game.sdk.GameCenterSDK;
import com.nearme.game.sdk.callback.SinglePayCallback;
import com.nearme.game.sdk.common.model.biz.PayInfo;
import com.nearme.platform.opensdk.pay.PayResponse;
import com.shushan.base.ActivityStackManager;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.home.bean.SectVipBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.pages.OpenVipSpendActivity;
import com.shushan.pages.bean.HuaweiPayBean;
import com.shushan.pages.bean.MaketingActivityBean;
import com.shushan.pages.bean.MaketingDataBean;
import com.shushan.pages.bean.OpenVipBean;
import com.shushan.pages.bean.OppoPayBean;
import com.shushan.pages.bean.WXPayBean;
import com.shushan.service.DialRTAService;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.BuildConfig;
import com.shushan.shushanbao.wxapi.WXPayEntryActivity;
import com.shushan.shushanbao.wxapi.bean.OrderBean;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: PayUtils
 * @Desciption: 联运支付工具类
 * @author: yichaohua
 * @date: 2018-11-19
 */
public class PayUtils {

    private static final String TAG = "PayUtils";

    private static int loopCount = 0;
    private final static int DELAY_TIMES = 1000*1;
    private static BaseActivity gContext;
    private static String outTradeNo;
    private static Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 0:
                    loopCount++;
                    querryOrder();
                    break;
            }
        }
    };

    public static void openVipHuawei(final BaseActivity context,final String url){
        if(TextUtils.isEmpty(url)){
            ToastUtils.showToast(context,"url为空");
            return;
        }

        loopCount = 0;
        gContext = context;
        context.showWaitDialog();


        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.HUAWEI_PAY_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("activity_vip_id",StringTools.getUrlParam(url,"activity_vip_id"))
                .putValue("pay_type","app")
                .putValue("goods_name",StringTools.getUrlParam(url,"goods_name"))
                .putValue("vip_time",StringTools.getUrlParam(url,"vip_time"));

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",StringTools.getUrlParam(url,"money"));
        }

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<HuaweiPayBean>() {

                    @Override
                    protected void onCommon() {
                        super.onCommon();

                        context.dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(HuaweiPayBean huaweiPayBean) {
                        if(huaweiPayBean != null){
                            PayReq payReq = new PayReq();

                            //商品名称
                            payReq.productName = huaweiPayBean.productName;
                            //商品描述
                            payReq.productDesc = huaweiPayBean.productDesc;
                            // 商户ID，来源于开发者联盟的“支付ID”
                            payReq.merchantId = huaweiPayBean.merchantId;
                            // 应用ID，来源于开发者联盟
                            payReq.applicationID = huaweiPayBean.applicationID;
                            // 支付金额
                            payReq.amount = huaweiPayBean.money;
                            // 商户订单号：开发者在支付前生成，用来唯一标识一次支付请求
                            payReq.requestId = huaweiPayBean.out_trade_no;
                            PayUtils.outTradeNo = huaweiPayBean.out_trade_no;
                            // 国家码
                            payReq.country = huaweiPayBean.country;
                            //币种
                            payReq.currency = huaweiPayBean.currency;
                            // 渠道号
                            payReq.sdkChannel = huaweiPayBean.sdkChannel;
                            // 回调接口版本号
                            payReq.urlVer = huaweiPayBean.urlVer;

                            // 商户名称，必填，不参与签名。开发者注册的公司名称
                            payReq.merchantName = huaweiPayBean.merchantName;

                            payReq.serviceCatalog = huaweiPayBean.serviceCatalog; // 应用设置为"X5"，游戏设置为"X6"
                            //商户保留信息，选填不参与签名，支付成功后会华为支付平台会原样 回调CP服务端
                            payReq.extReserved = huaweiPayBean.extReserved;

                            payReq.url = huaweiPayBean.url;
                            payReq.sign = huaweiPayBean.sign;

                            huaweiPay(context,payReq);
                        }else {
                            ToastUtils.showToast(context,"返回数据为空");
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.showNetError(netRetBean);
                    }
                });

    }


    public static void openVipHuawei(final BaseActivity context,MaketingDataBean maketingDataBean){
        if(maketingDataBean == null){
            ToastUtils.showToast(context,"分享营销数据为空");
            return;
        }

        List<MaketingActivityBean> maketingBeanList = maketingDataBean.getMaketingActivityBeanList();
        if(ToolUtils.isListEmpty(maketingBeanList)){
            ToastUtils.showToast(context,"活动信息为空");
            return;
        }
        if(maketingBeanList.size() < 2){
            ToastUtils.showToast(context,"10元购买7天VIP活动未开始");
            return;
        }
        MaketingActivityBean maketingActivityBean = maketingBeanList.get(1);

        loopCount = 0;
        gContext = context;
        context.showWaitDialog();

        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.HUAWEI_PAY_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("goods_name",maketingActivityBean.favorable_price + "元购买" + maketingActivityBean.vip_time + "天VIP活动")
                .putValue("vip_time",maketingActivityBean.vip_time)
                .putValue("activity_vip_id",maketingActivityBean.id);

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",maketingActivityBean.favorable_price);
        }

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<HuaweiPayBean>() {

                    @Override
                    protected void onCommon() {
                        super.onCommon();

                        context.dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(HuaweiPayBean huaweiPayBean) {
                        if(huaweiPayBean != null){
                            PayReq payReq = new PayReq();

                            //商品名称
                            payReq.productName = huaweiPayBean.productName;
                            //商品描述
                            payReq.productDesc = huaweiPayBean.productDesc;
                            // 商户ID，来源于开发者联盟的“支付ID”
                            payReq.merchantId = huaweiPayBean.merchantId;
                            // 应用ID，来源于开发者联盟
                            payReq.applicationID = huaweiPayBean.applicationID;
                            // 支付金额
                            payReq.amount = huaweiPayBean.money;
                            // 商户订单号：开发者在支付前生成，用来唯一标识一次支付请求
                            payReq.requestId = huaweiPayBean.out_trade_no;
                            PayUtils.outTradeNo = huaweiPayBean.out_trade_no;
                            // 国家码
                            payReq.country = huaweiPayBean.country;
                            //币种
                            payReq.currency = huaweiPayBean.currency;
                            // 渠道号
                            payReq.sdkChannel = huaweiPayBean.sdkChannel;
                            // 回调接口版本号
                            payReq.urlVer = huaweiPayBean.urlVer;

                            // 商户名称，必填，不参与签名。开发者注册的公司名称
                            payReq.merchantName = huaweiPayBean.merchantName;

                            payReq.serviceCatalog = huaweiPayBean.serviceCatalog; // 应用设置为"X5"，游戏设置为"X6"
                            //商户保留信息，选填不参与签名，支付成功后会华为支付平台会原样 回调CP服务端
                            payReq.extReserved = huaweiPayBean.extReserved;

                            payReq.url = huaweiPayBean.url;
                            payReq.sign = huaweiPayBean.sign;

                            huaweiPay(context,payReq);
                        }else {
                            ToastUtils.showToast(context,"返回数据为空");
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.showNetError(netRetBean);
                    }
                });

    }

    public static void openVipHuawei(final BaseActivity context,OpenVipBean selectVipBean){
        if(selectVipBean == null){
            ToastUtils.showToast(context,"活动信息为空");
            return;
        }

        loopCount = 0;
        gContext = context;
        context.showWaitDialog();

        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.HUAWEI_PAY_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("pay_type","app")
                .putValue("goods_name","开通超级VIP")
                .putValue("vip_time",selectVipBean.vip_time);

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",selectVipBean.cost_price);
        }

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<HuaweiPayBean>() {

                    @Override
                    protected void onCommon() {
                        super.onCommon();

                        context.dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(HuaweiPayBean huaweiPayBean) {
                        if(huaweiPayBean != null){
                            PayReq payReq = new PayReq();

                            //商品名称
                            payReq.productName = huaweiPayBean.productName;
                            //商品描述
                            payReq.productDesc = huaweiPayBean.productDesc;
                            // 商户ID，来源于开发者联盟的“支付ID”
                            payReq.merchantId = huaweiPayBean.merchantId;
                            // 应用ID，来源于开发者联盟
                            payReq.applicationID = huaweiPayBean.applicationID;
                            // 支付金额
                            payReq.amount = huaweiPayBean.money;
                            // 商户订单号：开发者在支付前生成，用来唯一标识一次支付请求
                            payReq.requestId = huaweiPayBean.out_trade_no;
                            PayUtils.outTradeNo = huaweiPayBean.out_trade_no;
                            // 国家码
                            payReq.country = huaweiPayBean.country;
                            //币种
                            payReq.currency = huaweiPayBean.currency;
                            // 渠道号
                            payReq.sdkChannel = huaweiPayBean.sdkChannel;
                            // 回调接口版本号
                            payReq.urlVer = huaweiPayBean.urlVer;

                            // 商户名称，必填，不参与签名。开发者注册的公司名称
                            payReq.merchantName = huaweiPayBean.merchantName;

                            payReq.serviceCatalog = huaweiPayBean.serviceCatalog; // 应用设置为"X5"，游戏设置为"X6"
                            //商户保留信息，选填不参与签名，支付成功后会华为支付平台会原样 回调CP服务端
                            payReq.extReserved = huaweiPayBean.extReserved;

                            payReq.url = huaweiPayBean.url;
                            payReq.sign = huaweiPayBean.sign;

                            huaweiPay(context,payReq);
                        }else {
                            ToastUtils.showToast(context,"返回数据为空");
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.showNetError(netRetBean);
                    }
                });

    }

    public static void sectVipHuawei(final BaseActivity context,SectVipBean selectVipBean){
        if(selectVipBean == null){
            ToastUtils.showToast(context,"活动信息为空");
            return;
        }

        loopCount = 0;
        gContext = context;
        context.showWaitDialog();

        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.SECT_HUAWEI_PAY_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("classification_vip_id",selectVipBean.id)
                .putValue("pay_type","app")
                .putValue("goods_name","开通专项练习VIP")
                .putValue("vip_time",selectVipBean.vip_day_time);

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",selectVipBean.promotion_price);
        }

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<HuaweiPayBean>() {

                    @Override
                    protected void onCommon() {
                        super.onCommon();

                        context.dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(HuaweiPayBean huaweiPayBean) {
                        if(huaweiPayBean != null){
                            PayReq payReq = new PayReq();

                            //商品名称
                            payReq.productName = huaweiPayBean.productName;

                            //商品描述
                            payReq.productDesc = huaweiPayBean.productDesc;
                            // 商户ID，来源于开发者联盟的“支付ID”
                            payReq.merchantId = huaweiPayBean.merchantId;
                            // 应用ID，来源于开发者联盟
                            payReq.applicationID = huaweiPayBean.applicationID;
                            // 支付金额
                            payReq.amount = huaweiPayBean.money;
                            // 商户订单号：开发者在支付前生成，用来唯一标识一次支付请求
                            payReq.requestId = huaweiPayBean.out_trade_no;
                            PayUtils.outTradeNo = huaweiPayBean.out_trade_no;
                            // 国家码
                            payReq.country = huaweiPayBean.country;
                            //币种
                            payReq.currency = huaweiPayBean.currency;
                            // 渠道号
                            payReq.sdkChannel = huaweiPayBean.sdkChannel;
                            // 回调接口版本号
                            payReq.urlVer = huaweiPayBean.urlVer;

                            // 商户名称，必填，不参与签名。开发者注册的公司名称
                            payReq.merchantName = huaweiPayBean.merchantName;

                            payReq.serviceCatalog = huaweiPayBean.serviceCatalog; // 应用设置为"X5"，游戏设置为"X6"
                            //商户保留信息，选填不参与签名，支付成功后会华为支付平台会原样 回调CP服务端
                            payReq.extReserved = huaweiPayBean.extReserved;

                            payReq.url = huaweiPayBean.url;
                            payReq.sign = huaweiPayBean.sign;

                            huaweiPay(context,payReq);
                        }else {
                            ToastUtils.showToast(context,"返回数据为空");
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.showNetError(netRetBean);
                    }
                });

    }

    public static void huaweiPay(final BaseActivity context,final PayReq payReq){
        context.showWaitDialog();

        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        if(isChildControl){
            context.setAllowBack(true);
            Intent intent = new Intent(context,DialRTAService.class);
            intent.putExtra("rtaType",4);
            intent.putExtra("allowPackName",Constants.ALLOW_HUAWEI_PAY_PACK_NAME);
            context.startService(intent);
        }

        HMSAgent.Pay.pay(payReq, new PayHandler() {
            @Override
            public void onResult(int retCode, PayResultInfo payInfo) {
                Log.i(TAG,"onResult() retCode=" + retCode);

                if (retCode == HMSAgent.AgentResultCode.HMSAGENT_SUCCESS && payInfo != null) {
                    boolean checkRst = PaySignUtil.checkSign(payInfo, Constants.HUAWEI_PAY_PUB_KEY);
                    Log.i(TAG,"huaweiPay() onResult: pay success and checksign=" + checkRst);
                    if (checkRst) {
                        // 支付成功并且验签成功，发放商品
                        ToastUtils.showToast(context,"支付成功");

                        HashMap<String,String> map = new HashMap<String,String>();
                        map.put("account",payReq.requestId);
                        map.put("money",payReq.amount);
                        UmengEventUtil.onEvent(BabyApplication.getInstance(),"open_vip_huawei_success_id",map);

                        context.dismissWaitDialog();
                        ActivityStackManager.getActivityStackManager().popTopActivity();
                    } else {
                        // 签名失败，需要查询订单状态：对于没有服务器的单机应用，调用查询订单接口查询；其他应用到开发者服务器查询订单状态。
                        mHandler.sendEmptyMessageDelayed(0,DELAY_TIMES);
                    }
                } else if (retCode == HMSAgent.AgentResultCode.ON_ACTIVITY_RESULT_ERROR
                        || retCode == PayStatusCodes.PAY_STATE_TIME_OUT
                        || retCode == PayStatusCodes.PAY_STATE_NET_ERROR) {
                    // 需要查询订单状态：对于没有服务器的单机应用，调用查询订单接口查询；其他应用到开发者服务器查询订单状态。
                    mHandler.sendEmptyMessageDelayed(0,DELAY_TIMES);
                }else if (retCode == HMSAgent.AgentResultCode.REQUEST_REPEATED){
                    ToastUtils.showToast(context,"发现您重复提交订单，请重新启动app后再购买");
                    context.dismissWaitDialog();
                    UmengEventUtil.onEvent(BabyApplication.getInstance(),"open_vip_huawei_faild_id","dispose");
                } else {
                    Log.i(TAG,"onResult() onResult: pay fail=" + retCode);
                    // 其他错误码意义参照支付api参考
                    ToastUtils.showToast(context,"支付失败");
                    context.dismissWaitDialog();
                    UmengEventUtil.onEvent(BabyApplication.getInstance(),"open_vip_huawei_faild_id","other");
                }
            }
        });

    }

    public static void querryOrder() {
        final BabyApplication babyApplication = BabyApplication.getInstance();

        if(loopCount >= 3){
            if(gContext != null){
                gContext.dismissWaitDialog();
            }


            ToastUtils.showToast(babyApplication,"支付失败");
            UmengEventUtil.onEvent(babyApplication,"open_vip_huawei_faild_id","unknow");

            return;
        }

        if(TextUtils.isEmpty(PayUtils.outTradeNo)){

            if(gContext != null){
                gContext.dismissWaitDialog();
            }

            ToastUtils.showToast(babyApplication,"订单号为空");
            UmengEventUtil.onEvent(babyApplication,"open_vip_huawei_faild_id","订单号为空");

            return;
        }

        UrlParse urlParse = new UrlCommParse(babyApplication, UrlConst.BASE_URL).appendRegion(UrlConst.QUERRY_ORDER);
        urlParse.putValue("out_trade_no",PayUtils.outTradeNo);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<OrderBean>() {

                    @Override
                    protected void onSuccess(OrderBean orderBean) {
                        HashMap<String,String> map = new HashMap<String,String>();
                        map.put("account",orderBean.phone);
                        map.put("money",orderBean.money+"");
                        UmengEventUtil.onEvent(babyApplication,"open_vip_huawei_success_id",map);
                        if(gContext != null){
                            gContext.dismissWaitDialog();
                        }

                        ToastUtils.showToast(babyApplication,"支付成功");

                        ActivityStackManager.getActivityStackManager().popTopActivity();
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        mHandler.sendEmptyMessageDelayed(0,DELAY_TIMES);
                    }
                });
    }

    public static void orderOppoSupperPay(final BaseActivity context,int vipId,int vipTime,int costPrice) {
        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.PAY_SUPPER_OPPO_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("pay_type","app")
                .putValue("goods_name","开通超级VIP")
                .putValue("vip_time",vipTime);

        if(vipId > 0){
            urlParse.putValue("activity_vip_id",vipId);
        }

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",costPrice);
        }

        context.showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<OppoPayBean>() {

                    @Override
                    protected void onSuccess(OppoPayBean oppoPayBean) {
                        openVipOppo(context,oppoPayBean);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.dismissWaitDialog();
                        context.showNetError(netRetBean);
                    }
                });
    }

    public static void orderOppoUrlPay(final BaseActivity context,final String url) {
        if(TextUtils.isEmpty(url)){
            ToastUtils.showToast(context,"url为空");
            return;
        }

        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.PAY_SUPPER_OPPO_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("activity_vip_id",StringTools.getUrlParam(url,"activity_vip_id"))
                .putValue("pay_type","app")
                .putValue("goods_name",StringTools.getUrlParam(url,"goods_name"))
                .putValue("vip_time",StringTools.getUrlParam(url,"vip_time"));

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",StringTools.getUrlParam(url,"money"));
        }

        context.showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<OppoPayBean>() {

                    @Override
                    protected void onSuccess(OppoPayBean oppoPayBean) {
                        openVipOppo(context,oppoPayBean);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.dismissWaitDialog();
                        context.showNetError(netRetBean);
                    }
                });
    }

    public static void orderOppoSectPay(final BaseActivity context,int vipId,int vipTime,int costPrice) {
        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.PAY_SECT_OPPO_ORDER);
        urlParse.putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("classification_vip_id",vipId)
                .putValue("pay_type","app")
                .putValue("goods_name","开通专项练习VIP")
                .putValue("vip_time",vipTime);

        if(BuildConfig.DEBUG){
            urlParse.putValue("money","0.01");
        }else {
            urlParse.putValue("money",costPrice);
        }

        context.showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<OppoPayBean>() {

                    @Override
                    protected void onSuccess(OppoPayBean oppoPayBean) {
                        openVipOppo(context,oppoPayBean);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        context.dismissWaitDialog();
                        context.showNetError(netRetBean);
                    }
                });
    }

    public static void openVipOppo(final BaseActivity context,OppoPayBean oppoPayBean) {
        context.dismissWaitDialog();

        if(oppoPayBean == null){
            return;
        }

        // cp支付参数
         // 支付金额，单位分
        PayInfo payInfo = new PayInfo(oppoPayBean.order, oppoPayBean.attach, oppoPayBean.amount);
        payInfo.setProductDesc(oppoPayBean.productDesc);
        payInfo.setProductName(oppoPayBean.productName);
        // 支付结果服务器回调地址，不通过服务端回调发货的应用可以不用填写~
        payInfo.setCallbackUrl(oppoPayBean.callbackUrl);

        GameCenterSDK.getInstance().doSinglePay(context, payInfo,
                new SinglePayCallback() {

                    @Override
                    public void onSuccess(String resultMsg) {
                        // add OPPO 支付成功处理逻辑~
                        ToastUtils.showToast(context,"支付成功");

                        ActivityStackManager.getActivityStackManager().popTopActivity();
                    }

                    @Override
                    public void onFailure(String resultMsg, int resultCode) {
                        // add OPPO 支付失败处理逻辑~
                        Log.e("ddd","resultMsg:"+resultMsg+" resultCode:"+resultCode);
                        if (PayResponse.CODE_CANCEL != resultCode) {
                            ToastUtils.showToast(context,"支付失败");
                        } else {
                            // 取消支付处理
                            ToastUtils.showToast(context,"支付取消");
                        }
                    }
                    @Override
                    public void onCallCarrierPay(PayInfo payInfo, boolean bySelectSMSPay) {
                        ToastUtils.showToast(context,"运营商支付");
                    }
                });
    }
}
